﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WanPiJie.Products;
using Itfort.Web;
using System.Text;
namespace WanPiJie.WebSite.Cmmdty
{
	public partial class Loader : Itfort.Web.PageBase
	{
		/// <summary>
		/// 商品类目Id
		/// </summary>
		public long CatalogId
		{
			get;
			private set;
		}

		/// <summary>
		/// 页码
		/// </summary>
		public int PageIndex
		{
			get;
			private set;
		}

		/// <summary>
		/// 默认的情况下每页返回12行商品信息
		/// </summary>
		public int PageSize
		{
			get
			{
				return 12;
			}
		}

		/// <summary>
		/// 跳过记录数
		/// </summary>
		public int Skip
		{
			get;
			private set;
		}

		/// <summary>
		/// 商品数据列表
		/// </summary>
        protected IList<Commodity> SalesInfoExpands
		{
			get;
			private set;
		}

		/// <summary>
		/// Get请求处理
		/// </summary>
		protected override void HttpGetProcess()
		{
			base.HttpGetProcess();
			this.Response.ContentType = "application/json";
			this.CatalogId = this.Request.ToInt64("cid");
			this.PageIndex = this.Request.ToInt32("pno");
			this.Skip = this.Request.ToInt32("sp");
			if (this.CatalogId != 0)
			{
				if (this.Skip != 0)
				{
					this.SalesInfoExpands = WpjSf.CommodityService.GetCommodities(this.CatalogId, this.Skip, this.PageIndex, this.PageSize);
				}
				else
				{
					this.SalesInfoExpands = WpjSf.CommodityService.GetCommodities(this.CatalogId, this.PageIndex, this.PageSize);
				}
			}
			else
			{
				if (this.Skip != 0)
				{
					this.SalesInfoExpands = WpjSf.CommodityService.GetCommodities(this.Skip, this.PageIndex, this.PageSize);
				}
				else
				{
					this.SalesInfoExpands = WpjSf.CommodityService.GetCommodities(this.PageIndex, this.PageSize);
				}
			}
			OutputJson();
		}


		/// <summary>
		/// 输出json格式的商品数据信息
		/// </summary>
		private void OutputJson()
		{
			string productFormat = "\"serialNumber\":{0},\"title\":\"{1}\",\"catalogId\":{2},\"picture\":\"{3}\",\"volume\":{4},\"supplier\":\"{5}\",\"url\":\"{6}\",\"price\":{7},\"maxAge\":{8},\"minAge\":{9},\"minWeight\":{10},\"maxWeight\":{11}";
			StringBuilder stringBuilder = new StringBuilder("[");
			foreach (var product in this.SalesInfoExpands)
			{
				stringBuilder.Append("{");
				stringBuilder.Append(string.Format(productFormat, product.SerialNumber, product.Title, product.CatalogId, product.Picture, product.Volume, product.Supplier, product.Url, product.Price, product.MaxAge, product.MinAge, product.MinWeight, product.MaxWeight));
				stringBuilder.Append("},");
			}
			stringBuilder.Remove(stringBuilder.Length - 1, 1);
			stringBuilder.Append("]");
			this.Response.Write(stringBuilder.ToString());
			this.Response.End();
		}
	}
}